package org.cocos2dx.javascript;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.KeyEvent;

import com.hb.api.HbAd;
import com.hb.api.HbAdEntry;
import com.hb.api.HbAdType;
import com.hb.api.HbSplashMgr;
import com.hbsdk.Ut;
import com.hbsdk.ad.HbAdError;
import com.hbsdk.ad.IHbAdListener;
import com.mt.util.ControlCenter;
import com.mt.util.MtPay;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;

public class MarsWrapper {
    private static MarsWrapper instace;
    public static MarsWrapper getInstance()
    {
        if(instace==null)
        {
            instace = new MarsWrapper();
        }
        return instace;
    }
    private String bannerCode = Config.BANNER;
    private String videoCode = Config.RewardId;
    private String feedCode = Config.NativeExpressId;
    private String fullCode = Config.FullScreenId;
    public AppActivity app;
    private HbAd hbBannerAd;
    private HbAd hbVideoAd;
    private HbAd fullVideoAd;
    private HbAd hbFeedAd;
    private boolean debug = true;
    private boolean firstLoad = true;
    private int feedCount=0;
    private static final int PERMISSION_REQ_ID = 22;
    private static final String[] REQUESTED_PERMISSIONS = {
            Manifest.permission.INTERNET,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };
    public void init(AppActivity a, Bundle savedInstanceState)
    {
        app = a;
        MtPay.start(app);
        HbAdEntry.onActivityCreate(app);
        HbSplashMgr.getInstance().onCreate(app, savedInstanceState);
    }
  

    public void register(String userName)
    {

    }

    public void login(String userName,String isNew)
    {
        videoInit();
        bannerInit();
        fullVideoInit();
        if(isNew=="1")
        {
            register(userName);
        }
        initFeed();
        loadVideoAd();
        loadFullVideoAd();
    }

    public void customEvent(String event)
    {
        //==========>customEvent
    }

    public void onStart() {
        ControlCenter.onStart(app);
    }

    public void onRestart() {
        ControlCenter.onRestart(app);
    }

    public void onResume() {
        ControlCenter.onResume(app);
        if (hbBannerAd != null) {
            hbBannerAd.onResume();
        }
        if (hbVideoAd != null) {
            hbVideoAd.onResume();
        }
        if(fullVideoAd!=null)
        {
            fullVideoAd.onResume();
        }
        HbSplashMgr.getInstance().onResume();
    }

    public void onPause() {
        ControlCenter.onPause(app);
        if (hbBannerAd != null) {
            hbBannerAd.onPause();
        }
        if (hbVideoAd != null) {
            hbVideoAd.onPause();
        }
        if(fullVideoAd!=null)
        {
            fullVideoAd.onPause();
        }
        HbSplashMgr.getInstance().onPause();
    }

    public void onStop() {
        ControlCenter.onStop(app);
        if (hbBannerAd != null) {
            hbBannerAd.onStop();
        }
        if (hbVideoAd != null) {
            hbVideoAd.onStop();
        }
        if(fullVideoAd!=null)
        {
            fullVideoAd.onStop();
        }
        HbSplashMgr.getInstance().onStop();
    }


    public void onDestroy() {
        ControlCenter.onDestroy(app);
        HbAdEntry.onDestory(app);
        if (hbBannerAd != null) {
            hbBannerAd.onDestory();
        }
        if (hbVideoAd != null) {
            hbVideoAd.onDestory();
        }
        if(fullVideoAd!=null)
        {
            fullVideoAd.onDestory();
        }
        HbSplashMgr.getInstance().onDestroy();
    }

    public void onNewIntent(Intent intent) {
        ControlCenter.onNewIntent(app, intent);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        ControlCenter.onActivityResult(app, requestCode, resultCode, data);
        HbAdEntry.onActivityResult(requestCode,resultCode,data);
        HbSplashMgr.getInstance().onActivityResult(requestCode, resultCode, data);
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        ControlCenter.onRequestPermissionsResult(app, requestCode, permissions, grantResults);
        HbAdEntry.onRequestPermissionsResult(requestCode,permissions,grantResults);
        HbSplashMgr.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);

    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return HbSplashMgr.getInstance().onKeyDown(keyCode, event);
    }

    private void bannerInit()
    {
        if (hbBannerAd == null) {
            hbBannerAd = new HbAd(app, new IHbAdListener() {
                @Override
                public void onAdShow() {
                    Log.d("WDNativePlatformTT","banner ad show");
                }

                @Override
                public void onAdFailed(HbAdError error) {
                    Log.d("WDNativePlatformTT","banner show fail:" + error.toString());
                }

                @Override
                public void onAdReady() {
                    Log.d("WDNativePlatformTT","banner ad ready");
                    setBannerInvisibility();
                    bannercount++;
                }

                @Override
                public void onAdClick() {
                    Log.d("TT","banner ad click");
                }

                @Override
                public void onAdDismissed() {
                    Log.d("WDNativePlatformTT","banner ad dismiss");
                    //**提示：当用户关闭banner广告回调此方法。接入方知道banner被关闭后可以在适当的时机重新展示banner，提高展示率。
                }

                @Override
                public void onAdReward() {
                }
            }, HbAdType.BANNER);
        }
        show1Banner();
    }

    private void show1Banner() {
        Log.d("WDNativePlatformTT","展示横幅广告---->");
        if (null != hbBannerAd) {
            hbBannerAd.showAd(bannerCode);//开始请求加载广告展示
        }
    }
    private int bannercount=1;
    //显示暂时隐藏的banner
    public void setBannerVisibility() {
        if(bannercount==0){
            show1Banner();
        }
        if (null != hbBannerAd) {
            hbBannerAd.setVisibility(true);//banner被隐藏后，再显示出来。
        }
    }
    //用于暂时隐藏banner
    public void setBannerInvisibility() {
        if (null != hbBannerAd) {
            hbBannerAd.setVisibility(false);//临时隐藏banner
        }
    }



    public void loadVideoAd() {
        //建议接入方在恰当的时机，在游戏层控制调用加载，比如：游戏开始时，进入有视频广告的界面等，按接入方实际情况调用。
        if (null != hbVideoAd) {
            hbVideoAd.loadAd(videoCode);
        }
    }
    private String adCompleted;
    private  void videoInit()
    {
        if (hbVideoAd == null) {
            hbVideoAd = new HbAd(app, new IHbAdListener() {
                @Override
                public void onAdShow() {
                    Log.d("WDNativePlatformTT","Video onAdShow...");
                }

                @Override
                public void onAdFailed(HbAdError error) { //不能在失败回调里调用loadAd，不然很可能会造成死循环。
                    Log.d("WDNativePlatformTT","Video onAdFailed:" + error.toString());
                    WDNativePlatform.callJS("fail");
                }

                @Override
                public void onAdReady() { //视频已经准备好了
                    Log.d("WDNativePlatformTT","Video onAdReady");
                }

                @Override
                public void onAdClick() {
                    Log.d("WDNativePlatformTT","Video onAdClick");
                }

                @Override
                public void onAdDismissed() {
                    Log.d("WDNativePlatformTT","Video onAdDismissed");
                    loadVideoAd();//接入方可以在这里reload，展示完重新加载。
                }

                @Override
                public void onAdReward() {//奖励视频这里发奖励
                    Log.d("WDNativePlatformTT","Video onAdReward");
                    adCompleted = "1";
                    WDNativePlatform.callJS("1");
                }
            }, HbAdType.REWARDVIDEO);
        }

        Log.d("TT","加载视频广告");
        hbVideoAd.loadAd(videoCode); //进入游戏开始第一次加载。
    }

    private long mTime = 0;//记录当前时间
    public void showVideoAd() {
        adCompleted = "0";
        if (null != hbVideoAd) {
            if (hbVideoAd.isReady()) { //判断是否加载完毕
                Log.d("WDNativePlatformTT","展示视频广告...");
                hbVideoAd.showAd(videoCode);//显示视频广告
            } else {
                //接入方也可以在这加载视频广告。但是要限制用户不能频繁点击，加载之间要有一定的时间间隔，视视频大小和网络情况大概在3s~5s。
                Ut.logI("video ad is not ready");

                long curTime = System.currentTimeMillis();
                boolean isLoad = (0 == mTime || curTime - mTime > 3000);//3s后可以再加载
                if (isLoad) {//这个是安卓层的防止频繁点击处理。如果接入方在游戏层做了类似的处理，可以将这里的限制注释。
                    mTime = curTime;
                    hbVideoAd.loadAd(videoCode);
                }
            }
        }
    }

    public void loadFullVideoAd() {
        //建议接入方在恰当的时机，在游戏层控制调用加载，比如：游戏开始时，进入有视频广告的界面等，按接入方实际情况调用。
        if (null != fullVideoAd) {
            fullVideoAd.loadAd(fullCode);
        }
    }
    private String fullCompleted;
    private void fullVideoInit()
    {
        if (fullVideoAd == null) {
            fullVideoAd = new HbAd(app, new IHbAdListener() {
                @Override
                public void onAdShow() {
                    Log.d("WDNativePlatformTT","Video onAdShow...");
                }

                @Override
                public void onAdFailed(HbAdError error) { //不能在失败回调里调用loadAd，不然很可能会造成死循环。
                    Log.d("WDNativePlatformTT","Video onAdFailed:" + error.toString());
//                    WDNativePlatform.callJS("fail");
                }

                @Override
                public void onAdReady() { //视频已经准备好了
                    Log.d("WDNativePlatformTT","Video onAdReady");
                }

                @Override
                public void onAdClick() {
                    Log.d("WDNativePlatformTT","Video onAdClick");
                }

                @Override
                public void onAdDismissed() {
                    Log.d("WDNativePlatformTT","Video onAdDismissed");
                    loadFullVideoAd();//接入方可以在这里reload，展示完重新加载。
                }

                @Override
                public void onAdReward() {//奖励视频这里发奖励
                    Log.d("WDNativePlatformTT","Video onAdReward");
//                    fullCompleted = "1";
//                    WDNativePlatform.callJS("1");
                }
            }, HbAdType.FULLVIDEO);
        }

        Log.d("WDNativePlatformTT","加载视频广告...");
        fullVideoAd.loadAd(fullCode); //进入游戏开始第一次加载。
    }
    private long mfullTime = 0;//记录当前时间
    public void showFullVideoAd() {
        fullCompleted = "0";
        if (null != fullVideoAd) {
            if (fullVideoAd.isReady()) { //判断是否加载完毕
                Log.d("WDNativePlatformTT","展示视频广告...");
                fullVideoAd.showAd(fullCode);//显示视频广告
            } else {
                //接入方也可以在这加载视频广告。但是要限制用户不能频繁点击，加载之间要有一定的时间间隔，视视频大小和网络情况大概在3s~5s。
                Ut.logI("video ad is not ready");

                long curTime = System.currentTimeMillis();
                boolean isLoad = (0 == mfullTime || curTime - mfullTime > 3000);//3s后可以再加载
                if (isLoad) {//这个是安卓层的防止频繁点击处理。如果接入方在游戏层做了类似的处理，可以将这里的限制注释。
                    mfullTime = curTime;
                    fullVideoAd.loadAd(fullCode);
                }
            }
        }
    }

    public void initFeed()
    {
        if (hbFeedAd == null) {
            hbFeedAd = new HbAd(app, new IHbAdListener() {
                @Override
                public void onAdShow() {
                    Log.d("WDNativePlatformTT", "feed ad show");
                }

                @Override
                public void onAdFailed(HbAdError error) {
                    Log.d("WDNativePlatformTT", "feed show fail:" + error.toString());
                }

                @Override
                public void onAdReady() {
                    if(MarsWrapper.getInstance().firstLoad){
                        setFeedInvisibility();
                        MarsWrapper.getInstance().firstLoad=false;
                    }
                    Log.d("WDNativePlatformTT", "feed ad ready");
                }

                @Override
                public void onAdClick() {
                    Log.d("WDNativePlatformTT", "feed ad click");
                }

                @Override
                public void onAdDismissed() {
                    Log.d("WDNativePlatformTT", "feed ad dismiss");
                    showFeedAd();
                }

                @Override
                public void onAdReward() {
                }
            }, HbAdType.FEED);
        }
        showFeedAd();
    }

    //开始加载广告展示 ，每次showAd调用都会重新加载广告
    private void showFeedAd() {
        Log.d("WDNativePlatformTT","展示feed广告---->");
        if (null != hbFeedAd) {
            hbFeedAd.showAd(feedCode);
        }
    }
    //显示被隐藏的广告view
    public void setFeedVisibility() {
        feedCount++;
        if(feedCount%3==0){
            showFeedAd();
        }
        if (null != hbFeedAd) {
            hbFeedAd.setVisibility(true);//接入方如果有暂时隐藏广告需求时，可设置广告是否可见
        }
    }
    //用于暂时隐藏广告view
    public void setFeedInvisibility() {
        if (null != hbFeedAd) {
            hbFeedAd.setVisibility(false);//接入方如果有暂时隐藏广告需求时，可设置广告是否可见
        }
    }
}
